<?php

	include_once "../library/DbConnection.php";
	include_once "../globals.php";

	class Box{
		
		
		/****************
		** CONSTRUCTOR **
		****************/
		public function __construct() {
			session_start();
			$this->db = new DbConnection($GLOBALS["db_server"], $GLOBALS["db_login"], $GLOBALS["db_pass"], $GLOBALS["db_name"]);
			$this->db->connect();
		}
		
		function saveBoxDatas($datas){
			if(!isset($_SESSION['user_id'])) return "session_error";
		 	$boxConfig = addslashes( $datas );
			
			$sql = "UPDATE logout_users SET gotBox = '".$boxConfig."' WHERE id = '".$_SESSION["user_id"]."'";
			$res = $this->db->query($sql);
			if( $res ) {
				return "ok";
			} else {
				return "error_sql";
			}
		}
		
		function loadBoxDatas($datas){
			$sql = "SELECT gotBox, virtual_id FROM logout_users WHERE id = '".$_SESSION["user_id"]."'";
			$res = $this->db->query($sql);
			$ult = mysql_fetch_array( $res );
			$sql2 = "SELECT gotBox FROM logout_users WHERE id = '".$ult[1]."'";
			$res2 = $this->db->query($sql2);
			$ult2 = mysql_fetch_array( $res2 );
			$ret = "<boxes>
<box userid=\"5mqpx6\">
	<item type=\"plante1\" x=\"3\" y=\"0\" color=\"0xffcc6600\" rotation=\"0\" class=\"TV\"/>
	<item type=\"commode\" x=\"0\" y=\"0\" color=\"0xffcc6666\" rotation=\"-270\"/>
	<item type=\"fauteuil\" x=\"3\" y=\"1\" color=\"0xffcc6666\" rotation=\"90\"/>
	<item type=\"table1\" x=\"1\" y=\"1\" color=\"0xff006633\" rotation=\"0\"/>
	<floorPaper source=\"moquette1.jpg\" scale=\"0.31\"/>
	<wallPaper source=\"fleurs2.jpg\" scale=\"0.67\"/>
	<postit type=\"sol\" source=\"tapis_enfant.jpg\" x=\"59.75\" y=\"90.65\" scale=\"1\"/>
	<postit type=\"mur\" source=\"keupon.png\" x=\"36.800000000000004\" y=\"26.05\" scale=\"1\"/>
	<postit type=\"mur\" source=\"picasso.jpg\" x=\"377\" y=\"26.450000000000003\" scale=\"1\"/>
</box>
<box userid=\"56ljfc\">
	<item type=\"table1\" x=\"1\" y=\"0\" color=\"0xffffffff\" rotation=\"0\"/>
	<item type=\"commode\" x=\"3\" y=\"2\" color=\"0xff006600\" rotation=\"180\"/>
	<item type=\"plante1\" x=\"0\" y=\"0\" color=\"0xffcc9900\" rotation=\"-270\"/>
	<item type=\"plante1\" x=\"3\" y=\"0\" color=\"0xffcc9900\" rotation=\"0\"/>
	<item type=\"fauteuil\" x=\"0\" y=\"2\" color=\"0xff009933\" rotation=\"-90\"/>
	<item type=\"table1\" x=\"1\" y=\"1\" color=\"0xffffffff\" rotation=\"0\"/>
	<floorPaper source=\"parquet1.jpg\" scale=\"0.67\"/>
	<wallPaper source=\"fleurs3.jpg\" scale=\"0.5\"/>
	<postit type=\"mur\" source=\"tapis_enfant.jpg\" x=\"53.050000000000004\" y=\"102.65\" scale=\"1\"/>
	<postit type=\"mur\" source=\"creart.png\" x=\"7.7\" y=\"32.85\" scale=\"1\"/>
	<postit type=\"mur\" source=\"picasso.jpg\" x=\"539.7\" y=\"20.700000000000003\" scale=\"1\"/>
	<postit type=\"mur\" source=\"keupon.png\" x=\"306.85\" y=\"13.15\" scale=\"1\"/>
</box>
<box userid=\"5aze6\">
	<item type=\"commode\" x=\"0\" y=\"1\" color=\"0xffffcccc\" rotation=\"0\"/>
	<item type=\"table1\" x=\"1\" y=\"2\" color=\"0xffff3366\" rotation=\"0\"/>
	<item type=\"fauteuil\" x=\"3\" y=\"2\" color=\"0xffffcccc\" rotation=\"90\"/>
	<item type=\"plante1\" x=\"3\" y=\"0\" color=\"0xffcc6600\" rotation=\"0\"/>
	<wallPaper source=\"fleurs.jpg\" scale=\"0.5\"/>
	<floorPaper source=\"parquet1.jpg\" scale=\"0.3\"/>
	<postit type=\"sol\" source=\"tapis_enfant.jpg\" x=\"48.550000000000004\" y=\"79.15\" scale=\"1\"/>
	<postit type=\"mur\" source=\"creart.png\" x=\"13.950000000000001\" y=\"12.4\" scale=\"1\"/>
	<postit type=\"mur\" source=\"picasso.jpg\" x=\"52.35\" y=\"108.2\" scale=\"1\"/>
	<postit type=\"mur\" source=\"creapup_1.png\" x=\"283.5\" y=\"13.05\" scale=\"1\"/>
</box>
<box editable=\"true\" userid=\"".$_SESSION["user_id"]."\">
	".$ult[0]."
</box>
<box userid=\"".$ult[1]."\">
	".$ult2[0]."
</box>
</boxes>";
			return $ret;
		}

		function saveBoxShot($stream){
			if(!isset($_SESSION['user_id'])) return "session_error";
		 			
			$bytearray = $stream["jpeg"];
			// bytearray is in the ->data property
			$imageData = $bytearray->data;
			$idimage = $_SESSION['user_id'].".jpg";
			
			return ( $success = file_put_contents($GLOBALS["users_datas"]."got/shots/".$idimage, $imageData) ) ? $idimage : $success;
		}

		/** Set box destroyed
		* @return	{Object}	:	"result" ::	true  -> Saved
		* 								false -> Error
		* 					"session" -> Session state (true/false);
		**/
		public function destroy()
		{
			$ret		= array();
			if(!isset($_SESSION['user_id']))
				$ret["session"]	= false;
			else{
				$res		= $this->db->query("UPDATE logout_Users SET gotBoxDestroyed='true' WHERE id='".$_SESSION['user_id']."'");
				$ret["session"]	= true;
				$ret["result"]		= $res;
			}
			return $ret;
		}
		
		
}
?>